import { RouteLocation, RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'
import Layout from '/@/layout/index.vue'
import store from '/@/store'
const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'login' */ '/@/views/login/index.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '', // 默认子路由
        name: 'home',
        component: () => import(/* webpackChunkName: 'home' */ '/@/views/home/index.vue')
      },
      {
        path: '/role',
        name: 'role',
        component: () => import(/* webpackChunkName: 'role' */ '/@/views/role/index.vue')
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import(/* webpackChunkName: 'menu' */ '/@/views/menu/index.vue')
      },
      {
        path: '/menu/create',
        name: 'menuCreate',
        component: () => import(/* webpackChunkName: 'menuCreate' */ '/@/views/menu/createOrEdit.vue')
      },
      {
        path: '/menu/edit/:id',
        name: 'menuEdit',
        component: () => import(/* webpackChunkName: 'menuCreate' */ '/@/views/menu/createOrEdit.vue')
      },
      {
        path: '/resource',
        name: 'resource',
        component: () => import(/* webpackChunkName: 'resource' */ '/@/views/resource/index.vue')
      },
      {
        path: '/course',
        name: 'course',
        component: () => import(/* webpackChunkName: 'course' */ '/@/views/course/index.vue')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName: 'user' */ '/@/views/user/index.vue')
      },
      {
        path: '/advert',
        name: 'advert',
        component: () => import(/* webpackChunkName: 'advert' */ '/@/views/advert/index.vue')
      },
      {
        path: '/addAdvertise',
        name: 'add-advertise',
        component: () => import(/* webpackChunkName: 'advert-space' */ '/@/views/advert/addOrEdit.vue')
      },
      {
        path: '/advert-space',
        name: 'advert-space',
        component: () => import(/* webpackChunkName: 'advert-space' */ '/@/views/advert-space/index.vue')
      },
      {
        path: '/role/:roleId/alloc-menu',
        name: 'alloc-menu',
        component: () => import(/* webpackChunkName: 'alloc-menu' */ '/@/views/role/alloc-menu.vue'),
        props: true // 将路由路径参数映射到组件的 props 数据中
      },
      {
        path: '/role/:roleId/alloc-resource',
        name: 'alloc-resource',
        component: () => import(/* webpackChunkName: 'alloc-menu' */ '/@/views/role/alloc-resource.vue'),
        props: true // 将路由路径参数映射到组件的 props 数据中
      },
      {
        path: '/course/create',
        name: 'course-create',
        component: () => import(/* webpackChunkName: 'course-create' */ '/@/views/course/create.vue')
      },
      {
        path: '/course/:courseId/edit',
        name: 'course-edit',
        component: () => import(/* webpackChunkName: 'course-edit' */ '/@/views/course/edit.vue'),
        props: true
      },
      {
        path: '/course/:courseId/section',
        name: 'course-section',
        component: () => import(/* webpackChunkName: 'course-section' */ '/@/views/course/section.vue'),
        props: true
      },
      {
        path: '/course/:courseId/video',
        name: 'course-video',
        component: () => import(/* webpackChunkName: 'course-video' */ '/@/views/course/video.vue'),
        props: true
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    // name: '404',
    component: () => import(/* webpackChunkName: '404' */ '/@/views/error-page/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // return true
  if (to.path !== '/login') {
    if (!store.state.user) {
      next({ name: 'login', query: { redirect: from.fullPath } })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
